package com.cyys.modules.core.sysParam;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cyys.common.redis.RedisUtil;
import com.cyys.modules.system.model.param.ParamBasic;
import com.cyys.modules.system.model.param.ParamBasicType;
import com.cyys.modules.system.service.param.ParamBasicService;
import com.cyys.modules.system.service.param.ParamBasicTypeService;
import org.springframework.data.redis.core.Cursor;
import org.springframework.data.redis.core.ScanOptions;
import org.springframework.stereotype.Component;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

@Component
public class ParamBasicUtil {

	public static void initParamBasic(ParamBasicService paramBasicService) {
		if(RedisUtil.hSize(RedisConstants.PARAM_BASIC_MAP) > 0){
			RedisUtil.delete(RedisConstants.PARAM_BASIC_MAP);
		}
		QueryWrapper<ParamBasic> qw = new QueryWrapper();
		qw.orderByAsc("sort");
		List<ParamBasic> paramBasicList = paramBasicService.listAll(qw);
		for (ParamBasic paramBasic : paramBasicList) {
			RedisUtil.hPut(RedisConstants.PARAM_BASIC_MAP,paramBasic.getId(),paramBasic.getName());
		}
	}

	public static Map<Object, Object> getParamBasic() {
		return RedisUtil.hGetAll(RedisConstants.PARAM_BASIC_MAP);
	}

	public static void initParamBasicType(ParamBasicTypeService paramBasicTypeService) {
		if(RedisUtil.size(RedisConstants.PARAM_BASIC_TYPE_MAP) > 0){
			RedisUtil.delete(RedisConstants.PARAM_BASIC_TYPE_MAP);
			RedisUtil.delete(RedisConstants.PARAM_BASIC_TYPE_LIST);
		}
		Map<String, Map<String, String>> paramBasicTypeMap = new LinkedHashMap<>();
		Map<String, List<ParamBasicType>> paramBasicTypeList = new LinkedHashMap<>();
		Cursor<Map.Entry<Object, Object>> curosr = RedisUtil.hScan(RedisConstants.PARAM_BASIC_MAP,
				ScanOptions.NONE);
		while(curosr.hasNext()){
			Map.Entry<Object, Object> entry = curosr.next();
			String basicId = entry.getKey().toString();
			List<ParamBasicType> basicTypeList = paramBasicTypeService.listByBasicId(basicId);
			Map<String, String> basicTypeMap = new LinkedHashMap<>();
			for (ParamBasicType paramBasicType : basicTypeList) {
				basicTypeMap.put(paramBasicType.getParamKey(), paramBasicType.getParamValue());
			}
			paramBasicTypeList.put(entry.getKey().toString(), basicTypeList);
			paramBasicTypeMap.put(entry.getKey().toString(), basicTypeMap);
		}
		RedisUtil.set(RedisConstants.PARAM_BASIC_TYPE_MAP,paramBasicTypeMap);
		RedisUtil.set(RedisConstants.PARAM_BASIC_TYPE_LIST,paramBasicTypeList);
	}

	public static Map<String, Map<String, String>> getParamBasicTypeMap() {
		return (Map<String, Map<String, String>>)RedisUtil.get(RedisConstants.PARAM_BASIC_TYPE_MAP);
	}

	public static Map<String, List<ParamBasicType>> getParamBasicTypeList() {
		return (Map<String, List<ParamBasicType>>) RedisUtil.get(RedisConstants.PARAM_BASIC_TYPE_LIST);
	}

	/**
	 * 两个和作一个，方便系统缓存管理里调用
	 * @param paramBasicService
	 * @param paramBasicTypeService
	 */
	public static void initParam(ParamBasicService paramBasicService, ParamBasicTypeService paramBasicTypeService) {
		initParamBasic(paramBasicService);
		initParamBasicType(paramBasicTypeService);
	}
	
}
